package class31To40;

public class TowerOfHanoi {

    public static void main(String[] args) {
        printHanoi(3);
        System.out.println("-------------------");
        hanoi(3);
    }

    public static void hanoi(int n) {
        if (n > 0) {
            f1(n, "左", "右", "中");
        }
    }

    public static void f1(int i, String from, String to, String other) {
        if (i == 1) {
            System.out.println("移动圆盘 1 从 " + from + " 到 " + to);
        } else {
            f1(i - 1, from, other, to);
            System.out.println("移动圆盘 " + i + " 从 " + from + " 到 " + to);
            f1(i - 1, other, to, from);
        }
    }


    private static void printHanoi(int n) {
        if (n > 0)
            f(n, "左", "右", "中");
    }

    private static void f(int n, String from, String to, String other) {
        if (n == 1) {
            System.out.println("移动圆盘 1从" + from + "移动到" + to);
            return;
        }
        f(n - 1, from, other, to);
        System.out.println("移动圆盘 " + n + "从" + from + "移动到" + to);
        f(n - 1, other, to, from);
    }
}
